package com.cppstudio.movie.fragment;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.cppstudio.movie.activity.R;
import com.cppstudio.movie.adapter.GridViewAdapter;
import com.cppstudio.movie.model.Movie;
import com.cppstudio.movie.util.ImageLoad;
import com.cppstudio.movie.util.ImageLoad.OnLoadImageListener;
import com.cppstudio.movie.widget.util.LineGridView;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FilmFragment1 extends Fragment{
     private GridViewAdapter adapter;
     private LineGridView gridview;
     private List<Movie> list=new  ArrayList<Movie>();
     private ExecutorService executorService = Executors.newFixedThreadPool(5); 
     private Handler handler = new Handler() {
 		public void handleMessage(Message msg) {
 			List<Movie> result = (List<Movie>) msg.obj;
 			
 			list.addAll(result);
 			
 			adapter.notifyDataSetChanged();
 		};
 	};
     @Override  
	    public void onActivityCreated(Bundle savedInstanceState) {  
	        // TODO Auto-generated method stub  
	        super.onActivityCreated(savedInstanceState);  
	       
	       
	    }  
	  
	    @Override  
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
	            Bundle savedInstanceState) {  
	    	 View view=inflater.inflate(R.layout.fragment_film_1, container, false);
	    	 gridview=(LineGridView)view.findViewById(R.id.gridview);
	    	// adapter=new GridViewAdapter(this.getActivity(),initData());
			// gridview.setAdapter(adapter);
	        adapter=new GridViewAdapter(this.getActivity(),list);
		    gridview.setAdapter(adapter);
	         Thread t=new Thread(run);
	         t.start();
	    	 return view; 
	    } 
	    
	   
	    
	    Runnable run=new Runnable(){
	    
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				Message msg = handler.obtainMessage();
				msg.obj =getJson();
				handler.sendMessage(msg);
				
				
			}
	    	
	    };
	    
	    
	    private List<Movie> getJson(){
	    	 List<Movie> list=new  ArrayList<Movie>();
	    	HttpClient client = new DefaultHttpClient();
	        HttpGet myget = new HttpGet("http://192.168.1.133/json.php");
	        try {
	            HttpResponse response = client.execute(myget);
	            String result = EntityUtils.toString(response.getEntity(),"GBK");  
	            JSONArray jsonArray=new JSONArray(result);
	            for(int i=0;i<jsonArray.length();i++){
	            	JSONObject jo=jsonArray.getJSONObject(i);
	            	final Movie item=new Movie();
	            	item.setScore(jo.getString("score"));
		    		item.setTitle(jo.getString("name"));
		    		item.setUrl(jo.getString("image"));
		    		for(int j=0;j<10;j++)
		    		  list.add(item);
	            }
	           
	        } catch (Exception e) {
	            Log.v("url response", "false");
	            e.printStackTrace();
	        }
	        return list;
	        }
	  
       
}
